package com.example.springbootwebflex.router;

import com.example.springbootwebflex.handler.QuoteHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

import static org.springframework.http.MediaType.*;
import static org.springframework.web.reactive.function.server.RequestPredicates.accept;
import static org.springframework.web.reactive.function.server.RequestPredicates.contentType;


/**
 * Created by IntelliJ IDEA.
 *
 * @author : 10779940c
 * @date 2024/9/2 13:30
 * @desc:
 */
@Configuration
public class QuoteRouter {

    @Bean
    public RouterFunction<ServerResponse> route(QuoteHandler quoteHandler) {
        return RouterFunctions
                .route(RequestPredicates.GET("/hello").and(accept(TEXT_PLAIN)), quoteHandler::hello)
                .andRoute(RequestPredicates.POST("/echo").and(accept(TEXT_PLAIN).and(contentType(TEXT_PLAIN)))
                        , quoteHandler::echo)
                .andRoute(RequestPredicates.GET("/quotes").and(accept(APPLICATION_JSON)),
                        quoteHandler::fetchQuotes)
                .andRoute(RequestPredicates.GET("/quotes").and(accept(APPLICATION_STREAM_JSON)),
                        quoteHandler::streamQuotes);
    }
}
